import java.util.Stack;

public class MyLinkedList implements IMyLinkedList{
    static class ListNode{
        public int value;
        public ListNode next;
        public ListNode prev;
        public ListNode(int value) {
            this.value = value;
        }
    }
    public ListNode head;
    public ListNode Last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            Last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
     }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            Last = node;
        }else {
            Last.next = node;
            node.prev = Last;
            Last = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //判断index合法性
        checkIndex(index);
        //处理特殊位置
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size() - 1) {
            addLast(data);
            return;
        }
        ListNode cur = search(index);
        ListNode node = new ListNode(data);
        //先链接正方向
        node.next = cur;
        cur.prev.next = node;
        //再链接反方向
        node.prev = cur.prev;
        cur.prev = node;
    }
    private ListNode search(int index) {
        ListNode cur = head;
        while (index > 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    private void checkIndex(int index) {
        if(index < 0 || index >= size()) {
            throw new IndexException("index下标不合法");
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            System.out.println("没有数据，无法删除");
        }
        ListNode cur = head;
        while (cur != null) {
            if(cur.value == key) {
                //开始删除
                //1、删除的是头节点
                if(cur == head) {
                    head = head.next;
                    //下面判断是为了防止链表只有一个节点，访问head.prev会空指针异常
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    //2、当删除的是尾节点
                    if(cur.next == null){
                        cur.prev.next = null;
                        Last = Last.prev;
                    }else {
                        //3、删除的是中间节点
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            System.out.println("没有数据，无法删除");
        }
        ListNode cur = head;
        while (cur != null) {
            if(cur.value == key) {
                //开始删除
                //1、删除的是头节点
                if(cur == head) {
                    head = head.next;
                    //下面判断是为了防止链表只有一个节点，访问head.prev会空指针异常
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    //2、当删除的是尾节点
                    if(cur.next == null){
                        cur.prev.next = null;
                        Last = Last.prev;
                    }else {
                        //3、删除的是中间节点
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                //return;不返回，继续开始遍历删除key值
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
        head = null;
        Last = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void redisplay() {
        ListNode cur = Last;
        while (cur != null) {
            System.out.print(cur.value+" ");
            cur = cur.prev;
        }
        System.out.println();
    }
    public void printList(ListNode head) {
        ListNode cur = head;
        if(head != null) {
            printList(cur.next);
            System.out.print(cur.value + " ");
        }
    }
    public void print(ListNode head) {
        ListNode cur = head;
        if(head == null) {
            return;
        }
        Stack<ListNode> stack = new Stack<>();
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()) {
            System.out.print(stack.pop().value + " ");
        }
        System.out.println();
    }
}
